package ThreadCooperation;

import chapter30.AccountWithSyncUsingLock;
import chapter30.ThreadCooperation;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadCredibility {
    private static credibility crea1 = new credibility();
    public static void main(String[] args) {
        Student s1 = new Student("王初冬","21068",300);
        //创建两个线程的线程池
        ExecutorService executor1 = Executors.newFixedThreadPool(2);
        executor1.execute(new IntegrityTask(s1.getIntegrityvalue()));
        executor1.execute(new minuteTask());
        executor1.shutdown();
        System.out.println("线程 1\t\t线程 2\t\t诚信分");
    }
    public static class IntegrityTask implements Runnable{
        private int num;

        public IntegrityTask(int num) {
            this.num = num;
        }

        @Override
        public void run() {
            try { // Purposely delay it to let the withdraw method proceed
                    crea1.increase(num);   //  增加该学生基础诚信分
                    Thread.sleep(1000);
                    crea1.increase(100);

            }
            catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    public static class minuteTask implements Runnable {
        public void run() {
            crea1.deduction(50);
            crea1.deduction(250);
            crea1.deduction(10);

        }
    }
    private static class credibility {
        // 创建一个新锁
        private  static Lock lock1 = new ReentrantLock();
        // 创建条件
        private  static Condition newDeposit1 = lock1.newCondition();



        private int integrityMinute = 0;   //
        public int getIntegrityMinute(){
            return integrityMinute;
        }


        // 减诚信分
        public void deduction(int amount){
            lock1.lock();   // 获取锁
            try {
                while (integrityMinute < amount){
                    System.out.println("\t\t\t该学生学分不足扣");
                    newDeposit1.await(); // 让当前线程等待，等待条件信号
                }
                integrityMinute -=amount;
                System.out.println("\t\t\t诚信分减少："+amount+"\t\t" + getIntegrityMinute());
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            finally {
                lock1.unlock(); // 释放锁
            }
        }

        //  增加诚信分
        public void increase(int amount){
            lock1.lock();  // 获取锁
            try {
                integrityMinute += amount;
                System.out.println("增加诚信分"+amount+"\t\t\t\t\t" + getIntegrityMinute());

                // 等待条件的信号线程
                newDeposit1.signalAll();
            }
            finally {
                lock1.unlock();   // 释放锁
            }
        }
    }
}

